clear all

// SET MACROS
global Input  "Y:/limited/Michigan_CTE/funding_change/data_final"
global Output "Y:/limited/Michigan_CTE/funding_change/output/figures"

qui include "Y:/limited/Michigan_CTE/funding_change/code/analyze/figures/00_colors.do"

local demographics "female black hisp another_race sped lep econdis"
local academics "test_avg_g8 test_avg_sq_g8 test_avg_g8_imp attendance_g8 attendance_g8_imp"

// LOAD DATA
use $Input/dcode_funding.dta, clear

keep if inrange(year,2012, 2019)

foreach v in cte_funds_state60 cte_funds_cepd40 cte_funds_total {
	gen all_`v'_n = dcode_`v' + isd_`v'_n
}

foreach v of varlist all_cte_funds_* {
	bys year: gegen all_`v' = total(`v')

	bys dcode: gegen pre_`v'  = mean(cond(year<2015,`v',.))
	bys dcode: gegen post_`v' = mean(cond(year>=2017,`v',.))

	gen pct_`v' = (100 * `v') / all_`v'
	
	bys dcode: gegen pre_pct_`v'  = mean(cond(year<2015,pct_`v',.))
	bys dcode: gegen post_pct_`v' = mean(cond(year>=2017,pct_`v',.))

	gen ps_`v' = `v' / hs_students
	
	bys dcode: gegen pre_ps_`v'  = mean(cond(year<2015,`v'/hs_students,.))
	bys dcode: gegen post_ps_`v' = mean(cond(year>=2017,`v'/hs_students,.))
}

foreach v of varlist pre* post* {
	replace `v' = 0 if missing(`v')
}

forval i = 2012/2019 {
	gen c`i' = year == `i' 
}
replace c2014 = 0

// RUN REGRESSIONS
gen cohort = .
gen fund0_over_time      = .
gen fund0_over_time_l95  = .
gen fund0_over_time_u95  = .
gen fund1_over_time      = .
gen fund1_over_time_l95  = .
gen fund1_over_time_u95  = .
gen funds0_over_time     = .
gen funds0_over_time_l95 = .
gen funds0_over_time_u95 = .
gen funds1_over_time     = .
gen funds1_over_time_l95 = .
gen funds1_over_time_u95 = .
gen fundc0_over_time     = .
gen fundc0_over_time_l95 = .
gen fundc0_over_time_u95 = .
gen fundc1_over_time     = .
gen fundc1_over_time_l95 = .
gen fundc1_over_time_u95 = .

foreach n in 0 1 {
	eststo: reghdfe ps_all_cte_funds_total c2012-c2019 if district_most_poor == `n' [aw=hs_students], a(dcode) cluster(dcode)
	local i = 1
	forvalues x = 2012/2019 {
	  replace fund`n'_over_time =  _b[c`x'] in `i'
	  replace fund`n'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace fund`n'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace cohort = `x' if cohort==. in `i'
	  local ++i
	}
	
	eststo: reghdfe ps_all_cte_funds_state c2012-c2019 if district_most_poor == `n' [aw=hs_students], a(dcode) cluster(dcode)
	local i = 1
	forvalues x = 2012/2019 {
	  replace funds`n'_over_time =  _b[c`x'] in `i'
	  replace funds`n'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace funds`n'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  local ++i
	}

	eststo: reghdfe ps_all_cte_funds_cepd c2012-c2019 if district_most_poor == `n' [aw=hs_students], a(dcode) cluster(dcode)
	local i = 1
	forvalues x = 2012/2019 {
	  replace fundc`n'_over_time =  _b[c`x'] in `i'
	  replace fundc`n'_over_time_l95 =  _b[c`x'] - (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  replace fundc`n'_over_time_u95 =  _b[c`x'] + (invttail(e(df_r),0.025) *  _se[c`x']) in `i'
	  local ++i
	}
	
}

keep cohort fund0* fund1* funds0* funds1* fundc0* fundc1* 
drop if missing(cohort)

// DRAW GRAPH 
tw (rarea fund0_over_time_l95 fund0_over_time_u95 cohort, color("$c2%30") lwidth(none)) ///
   (rarea fund1_over_time_l95 fund1_over_time_u95 cohort, color("$c1%30") lwidth(none)) ///
   (connected fund0_over_time fund1_over_time cohort, sort ///
   mc("$c2" "$c1") ///
   m(T O) ///
   lc("$c2" "$c1") ///
  lp(dash_dot shortdash dash solid)), ///
   scale(1.1) ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(53.1 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(57. 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle(" " "Cohort Graduation Year", margin(t=2)) ///
   ylabel(-20 "-$20" 0 "$0" 20 "$20" 40 "$40" 60 "$60", angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax gmin) ///
   ytitle("Change in Per-Student CTE Funding" " ") ///
   legend(order(4 "Poorest 25% of Districts" 3 "Wealthiest 75% of Districts") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7) ysize(3.25)  ///
	 scheme(s2color)

// EXPORT GRAPH
graph save $Output/06b_reg_dollar_wealth.gph, replace
*graph export $Output/reg_dollar_wealth.png, replace





// DRAW GRAPH 
tw (rarea fundc0_over_time_l95 fundc0_over_time_u95 cohort, color("$c2%30") lwidth(none)) ///
   (rarea fundc1_over_time_l95 fundc1_over_time_u95 cohort, color("$c1%30") lwidth(none)) ///
   (connected fundc0_over_time fundc1_over_time cohort, sort ///
   mc("$c2" "$c1") ///
   m(T O) ///
   lc("$c2" "$c1") ///
  lp(dash_dot shortdash dash solid)), ///
   scale(1.1) ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(53.1 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(57. 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle(" " "Cohort Graduation Year", margin(t=2)) ///
   ylabel(-20 "-$20" 0 "$0" 20 "$20" 40 "$40" 60 "$60", angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax gmin) ///
   ytitle("Change CEPD Discressionary Funding (40%)" " ") ///
   legend(order(4 "Poorest 25% of Districts" 3 "Wealthiest 75% of Districts") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7) ysize(3.25)  ///
	 scheme(s2color)

	
graph save $Output/B06a_reg_dollar40_wealth.gph, replace 


// DRAW GRAPH 
tw (rarea funds0_over_time_l95 funds0_over_time_u95 cohort, color("$c2%30") lwidth(none)) ///
   (rarea funds1_over_time_l95 funds1_over_time_u95 cohort, color("$c1%30") lwidth(none)) ///
   (connected funds0_over_time funds1_over_time cohort, sort ///
   mc("$c2" "$c1") ///
   m(T O) ///
   lc("$c2" "$c1") ///
  lp(dash_dot shortdash dash solid)), ///
   scale(1.1) ///
   xline(2014.5 2015.5, lc(gs12) lp(-)) ///
   yline(0, lc(gs11)) ///
   text(53.1 2014.5 "Funding" "increased," "new formula" "announced", placement(0) color(black) box bcolor(white) margin(b=1)) ///
   text(57. 2015.5 "New" "formula" "applied", placement(0) color(black) box bcolor(white) margin(t=2 b=1)) ///
   xlabel(2012/2019) ///
   xtitle(" " "Cohort Graduation Year", margin(t=2)) ///
   ylabel(-20 "-$20" 0 "$0" 20 "$20" 40 "$40" 60 "$60", angle(0) glc(gs13) glw(vthin) glp(shortdash) gmax gmin) ///
   ytitle("Change State Formula Funding (60%)" " ") ///
   legend(order(4 "Poorest 25% of Districts" 3 "Wealthiest 75% of Districts") rows(1) symxsize(7) region(lstyle(none))) ///
   graphregion(color(white)) bgcolor(white) ///
   xsize(7) ysize(3.25)  ///
	 scheme(s2color)
	 


graph save $Output/B06b_reg_dollar60_wealth.gph, replace 

